﻿/**
 * Created by acer on 2015/3/19.
 */
wetherDevelopement = false;
//获取url内？后内容
var request = {
    QueryString : function(val){
        var uri = window.location.search;
        var re = new RegExp( "" + val + "=([^&?]*)", "ig" );
        return ( ( uri.match( re ) ) ? decodeURIComponent( uri.match( re )[0].substr( val.length + 1 ) ) : null );
    }/*,
    QueryHash: function(val){
        var uri = window.location.hash;
        var re = new RegExp("" +val+ "=([^#?]*)", "ig");
        return ((uri.match(re))?decodeURIComponent(uri.match(re)[0].substr(val.length+1)):null);
    }*/
};
function getAjax( url, params, callback ){
    var param = {
        url : appSocket + url + '?' + decodeURIComponent( $.param( params ) ).replace( /'/, '' ),
        data : {},
        method : 'get'
    }
    ajaxRequest( param, callback )
}
function postAjax( url, data, callback ){
    var param = {
        url : appSocket + url,
        data : data,
        method : 'post'
    }
    ajaxRequest( param, callback )
}
AppConfig = function( judge ){
    var _me = this
    var appSocketJudegeFunc = function( innerJudge ){
        var appSocketJudegeFuncInnerUrl
        if( innerJudge ){
            //TODO 跨域ajax
            appSocketJudegeFuncInnerUrl =  'http://10.0.0.6:3000';//外网
        }
        else{
            //TODO 本地服务ajax
            appSocketJudegeFuncInnerUrl = '';
        }
        return appSocketJudegeFuncInnerUrl
    }
    _me.judgement = judge
    _me.appSocket = appSocketJudegeFunc( judge )
    _me.ajaxRequest = function( params , CallBack ){
        var innerUrl
        var innerData
        var innerMethod

        if( _me.judgement ){
            //TODO 跨域ajax
            innerUrl    =  '/commonAction/ajaxRequest';
            innerData   = params;
            innerMethod = 'post';
        }
        else{
            //TODO 本地服务ajax
            innerUrl       = params.url;
            innerData      = params.data ;
            innerMethod    = params.method;
        }
        //console.log( innerData )
        $.ajax({
            url  : innerUrl,
            data : innerData,
            type : innerMethod,
            async: true,
            dataType : "json",
            success : function( data ){
                //console.log( 2)
                CallBack( data )
            },
            timeout : 30 * 60 * 1000,
            error : function( err ){
                if( err.statusText == 'timeout' ) {
                    alert("请求超时，请刷新页面");
                    this.abort();
                }
            }
        });
    }

};
var specialAppConfig = new AppConfig( wetherDevelopement );
ajaxRequest = specialAppConfig.ajaxRequest;
appSocket = specialAppConfig.appSocket;
//获取当前时间
getNow = function( fmt ){
    return new Date().Format( fmt )
};

Date.prototype.Format = function(fmt){
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor( (this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt = fmt.replace( RegExp.$1 , ( this.getFullYear() + "" ).substr( 4 - RegExp.$1.length ) );
    for(var k in o)
        if( new RegExp("("+ k +")").test( fmt ) )
            fmt = fmt.replace( RegExp.$1 , ( RegExp.$1.length == 1 ) ? ( o[k] ) : ( ( "00" + o[k] ).substr( ( "" + o[k]).length ) ) );
    fmt = fmt.indexOf('NaN') > -1 ? '' : fmt
    return fmt;
};
//手机格式验证，用于if类型判断
function phoneFormTest(phone){
    var reg = /^(1[34578][0-9]{9})/
    //var reg=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)|(13\d{9}$)|(15[0135-9]\d{8}$)|(18[267]\d{8}$)|(^[0-9]{3,4}\-[0-9]{3,4}\-[0-9]{3,4}$)/
    if(reg.test( phone )){
     return true;
 }
    else {
     return false;
 }
}
//邮箱格式验证，用于if类型判断
function emailFormTest(email){
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(reg.test(email)){
        return true;
    }
    else {
        return false;
    }
}
/***cookie内JSON转化为数组**/
function json_decode(str_json) {
    var json = this.window.JSON;
    if (typeof json === 'object' && typeof json.parse === 'function') {
        return json.parse(str_json);
    }
    var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
    var j;
    var text = str_json;
    cx.lastIndex = 0;
    if (cx.test(text)) {
        text = text.replace(cx, function (a) {
            return '\\u' +
                ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
        });
    }
    if (/^[\],:{}\s]*$/.
            test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
                replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
                replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
        j = eval('(' + text + ')');

        return j;
    }

    // If the text is not JSON parseable, then a SyntaxError is thrown.
    throw new SyntaxError('json_decode');
}